package DataStructure;

import java.util.Scanner;

/**
 * @time: 2021/3/18/018 9:07
 * @author: Zhurunhe
 */
public class CircleQueueTest {
    public static void main(String[] args) {

        CircleQueue circleQueue = new CircleQueue(3);
        Scanner scanner = new Scanner(System.in);
        char key = ' ';

        boolean loop = true;
        while (loop) {
            System.out.println("a:添加数据到队列中");
            System.out.println("g:从队列中取数据");
            System.out.println("h:获取队列中的头数据");
            System.out.println("s:显示队列中的数据");
            System.out.println("e:退出");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    try {
                        circleQueue.show();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }

                    break;
                case 'h':
                    try {
                        System.out.println(circleQueue.getHead());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }

                    break;
                case 'a':
                    System.out.println("请输入一个数值");
                    circleQueue.addToCQueue(scanner.nextInt());
                    break;
                case 'g':
                    try {
                        System.out.println(circleQueue.getData());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }

                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }
}

class CircleQueue {
    private int[] CQueue;
    private int rear, front;
    private int maxSize;

    public CircleQueue(int maxSize) {
        this.maxSize = maxSize;
        CQueue = new int[maxSize];
        rear = 0;
        front = 0;
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public boolean isFull() {
        return front == (rear + 1) % maxSize;
    }

    public void addToCQueue(int n) {
        if (isFull()) {
            System.out.println("队列满，不能添加数据");
            return;
        }
        CQueue[rear] = n;
        rear = (rear + 1) % maxSize;
    }

    public int getData() {
        if (isEmpty()) {
            throw new RuntimeException("数组为空，没有数据可以取出");
        }
        int value = CQueue[front];
        front = (front + 1) % maxSize;
        return value;
    }

    public int getHead() {
        if (isEmpty()) {
            throw new RuntimeException("数组为空，没有数据可以取出");
        }
        return CQueue[front];
    }

    public void show() {
        if (isEmpty()) {
            throw new RuntimeException("数组为空，没有数据可以取出");
        }
        for (int i = front; i < front + (rear + maxSize - front) % maxSize; i++) {
            int index=(i % maxSize);
            System.out.println(index+"---> " + CQueue[index]);
        }
    }
}
